/**
 * Created by Kate.Lo on 2018/3/18.
 * actions通常是两种情况：
 * 异步操作、
 * 对mutations的封装（如果一个动作需要出发多个mutation的时候，可以用action将其进行封装，达到一次修改多个数据的目的）
 */
import * as types from './mutation-type'
import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'
import {saveSearch, deleteSearch, clearSearch, savePlay, saveFavorite, deleteFavorite} from 'common/js/cache'

function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}

export const selectPlay = function ({commit, state}, {list, index}) {
  commit(types.SET_SEQUENCE_LIST, list)
  if (state.mode === playMode.random) {
    let randomList = shuffle(list)
    commit(types.SET_PLAYLIST, randomList)
    index = findIndex(randomList, list[index])
  } else {
    commit(types.SET_PLAYLIST, list)
  }
  commit(types.SET_CURRENT_INDEX, index)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}

export const randomPlay = function ({commit}, {list}) {
  commit(types.SET_PLAY_MODE, playMode.random)
  commit(types.SET_SEQUENCE_LIST, list)
  let randomList = shuffle(list)
  commit(types.SET_PLAYLIST, randomList)
  commit(types.SET_CURRENT_INDEX, 0)
  commit(types.SET_PLAYING_STATE, true)
}

export const insertSong = function ({commit, state}, song) {
  // .slice()用来设置副本，避免直接修改state中的值，对象是引用
  let playList = state.playList.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  // 记录当前歌曲
  let currentSong = playList[currentIndex]
  // 查找当前列表中是否已经存在这首歌曲，并返回其索引
  let fpIndex = findIndex (playList, song)
  // 插入歌曲，索引+1
  currentIndex++
  // 插入这首歌曲到当前正在播放的后面
  playList.splice(currentIndex, 0, song)
  // 如果已经包含这首歌曲了
  if (fpIndex > -1) {
    // 如果存在的歌曲在前面
    if (currentIndex < fpIndex) {
      playList.splice(fpIndex + 1, 1)
    } else {
      // 如果存在的歌曲在后面
      playList.splice(fpIndex, 1)
      currentIndex--
    }
  }
  // 插入歌曲，索引+1
  let currentSIndex = findIndex (sequenceList, currentSong) + 1
  let fsIndex = findIndex (sequenceList, song)
  // 插入这首歌曲到当前正在播放的后面
  sequenceList.splice(currentSIndex, 0, song)
  // 如果已经包含这首歌曲了
  if (fsIndex > -1) {
    // 如果存在的歌曲在前面
    if (currentSIndex < fsIndex) {
      sequenceList.splice(fsIndex + 1, 1)
    } else {
      // 如果存在的歌曲在后面
      sequenceList.splice(fsIndex, 1)
    }
  }
  commit(types.SET_PLAYLIST, playList)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}

export const saveSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, saveSearch(query))
}

export const deleteSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, deleteSearch(query))
}

export const clearSearchHistory = function ({commit}) {
  commit(types.SET_SEARCH_HISTORY, clearSearch())
}

export const deletePlayList = function ({commit, state}, song) {
  let playList = state.playList.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  let pIndex = findIndex(playList, song)
  playList.splice(pIndex, 1)
  let sIndex = findIndex(sequenceList, song)
  sequenceList.splice(sIndex, 1)

  if (currentIndex > pIndex || currentIndex === playList.length) {
    currentIndex--
  }

  commit(types.SET_PLAYLIST, playList)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)

  const playSate = playList.length > 0
  commit(types.SET_PLAYING_STATE, playSate)
}

export const clearPlayList = function ({commit}) {
  commit(types.SET_PLAYLIST, [])
  commit(types.SET_SEQUENCE_LIST, [])
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAYING_STATE, false)
}

export const savePlayHistory = function ({commit}, song) {
  commit(types.SET_PLAY_HISTORY, savePlay(song))
}

export const saveFavoriteList = function({commit}, song) {
  commit(types.SET_FAVORITE_LIST, saveFavorite(song))
}

export const deleteFavoriteList = function({commit}, song) {
  commit(types.SET_FAVORITE_LIST, deleteFavorite(song))
}
